package com.taiji.dicom4cloud.smart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.util.StringUtil;
import com.taiji.dicom4cloud.common.model.entity.SysDictItem;
import com.taiji.dicom4cloud.smart.model.entity.CarOwer;
import com.taiji.dicom4cloud.smart.model.entity.Org;
import com.taiji.dicom4cloud.smart.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ReportServiceImpI
 * @Description
 * @Author HX
 * @Date 2021/8/13 17:31
 **/
@Service
public class ReportServiceImpI implements ReportService {

    @Autowired
    private OrgService orgService;

    @Autowired
    private CarOwerService carOwerService;

    @Autowired
    private CarService carService;

    @Autowired
    private AlarmRecordService alarmRecordService;

    @Autowired
    private RfidEquipmentService rfidEquipmentService;

    @Autowired
    private AlarmEquipmentService alarmEquipmentService;

    @Autowired
    private CameraEquipmentService cameraEquipmentService;

    @Autowired
    private SysDictItemService sysDictItemService;

    @Override
    public Map<String, Integer> getBusinessTotal(String orgId) {
        Map<String,Integer> map = new HashMap<>();
        QueryWrapper queryWrapper= Wrappers.query();
        QueryWrapper carOwerQueryWrapper= Wrappers.query();
        QueryWrapper carWrapper= Wrappers.query();

        QueryWrapper alarmWrapper = Wrappers.query();
        QueryWrapper alarmEquipWrapper = Wrappers.query();
        QueryWrapper alarmRfidWrapper = Wrappers.query();
        QueryWrapper cameraRfidWrapper = Wrappers.query();

        carWrapper.eq("del_flag","0");
        queryWrapper.eq("del_flag","0");
        carOwerQueryWrapper.eq("del_flag","0");
        alarmRfidWrapper.eq("del_flag","0");
        alarmEquipWrapper.eq("del_falg","0");
        cameraRfidWrapper.eq("del_falg","0");

        if(StringUtil.isNotEmpty(orgId)&&!orgId.equals("-1")){
            carOwerQueryWrapper.eq("org_id",orgId);
            carWrapper.eq("org_id",orgId);
            alarmWrapper.eq("org_id",orgId);
            alarmRfidWrapper.eq("org_id",orgId);
            alarmEquipWrapper.eq("org_id",orgId);
            cameraRfidWrapper.eq("org_id",orgId);
        }

        map.put("ownerTotal",carOwerService.count(carOwerQueryWrapper));
        map.put("orgTotal",orgService.count(queryWrapper));
        map.put("carTotal",carService.count(carWrapper));
        map.put("alarmTotal",alarmRecordService.count(alarmWrapper));
        map.put("rfidTotal",rfidEquipmentService.count(alarmRfidWrapper));
        map.put("electricTotal",alarmEquipmentService.count(alarmEquipWrapper));
        map.put("cameraTotal",cameraEquipmentService.count(cameraRfidWrapper));
        return map;
    }


    @Override
    public Map<String, Object> getOrgCarTotal() {
        List<Org> orgList = orgService.list(Wrappers.<Org>lambdaQuery().eq(Org::getDelFlag,"0"));
        Map<String,Object> map = new HashMap<>();
        List<String> orgNames = new ArrayList<>();
        List<Integer> integers = new ArrayList<>();
        for(Org org: orgList){
            QueryWrapper carWrapper=Wrappers.query();
            carWrapper.eq("del_flag","0");
            carWrapper.eq("org_id",org.getOrgId());
            orgNames.add(org.getOrgName());
            integers.add(carService.count(carWrapper));
        }
        map.put("xAxis",orgNames);
        map.put("series",integers);
        return map;
    }


    @Override
    public Map<String, Object> getAlarmReport() {
        List<Org> orgList = orgService.list(Wrappers.<Org>lambdaQuery().eq(Org::getDelFlag,"0"));
        Map<String,Object> map = new HashMap<>();
        List<String> orgNames = new ArrayList<>();
        List<Integer> integers = new ArrayList<>();
        for(Org org: orgList){
            QueryWrapper alarmWrapper = Wrappers.query();
            alarmWrapper.eq("flag","0");
            alarmWrapper.eq("org_id",org.getOrgId());
            orgNames.add(org.getOrgName());
            integers.add(alarmRecordService.count(alarmWrapper));
        }
        map.put("xAxis",orgNames);
        map.put("series",integers);
        return map;
    }

    @Override
    public Map<String, Object> getAlarmDistribution(String orgId) {




        List<String> names = new ArrayList<>();
        List<Integer> integers = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();

        List<SysDictItem> sysDictItems = sysDictItemService.list(
                Wrappers.<SysDictItem>query().lambda()
                        .eq(SysDictItem::getType, "equ_type")
                        .orderByDesc(SysDictItem::getSort));


        for(SysDictItem sysDictItem:sysDictItems){
            names.add(sysDictItem.getLabel());
            QueryWrapper alarmWrapper = Wrappers.query();
            alarmWrapper.eq("type",sysDictItem.getValue());
            if(StringUtil.isNotEmpty(orgId)){
                alarmWrapper.eq("org_id",orgId);
            }

            integers.add(alarmRecordService.count(alarmWrapper));
        }


        map.put("xAxis",names);
        map.put("series",integers);

        return map;
    }
}
